<?php


/**
 * 基类Action
 * Enter description here ...
 * @author Administrator
 *
 */
class CommonAction extends Action{
	
	/**
	 * 初始化进行RBAC权限认证
	 * Enter description here ...
	 */
	function _initialize() {   //获取RBAC相关配置 config.php
		if (C ( 'USER_AUTH_ON') &&!in_array(MODULE_NAME,explode(',',C('NOT_AUTH_MODULE')))) {
			
			import ( '@.ORG.RBAC');     //导入RBAC权限控制
			
			if (!RBAC::AccessDecision ()) {
				if (!$_SESSION [C ( 'USER_AUTH_KEY')]) {
					$this->redirect ( C ( 'USER_AUTH_GATEWAY') );
				}
				$this->error('没有权限！');
			}
		}
		
		$goods = D('Goods');    
		$goodsdata = $goods->order('id desc')->find();
		if($goodsdata['id'] >= 50){
			checkKey();
		}
	}
	
		
	/**
	 * 
	 * Enter description here ...
	 */
	function forbid(){
		$data = Init_GP(array('id'));
		$model	=	D($this->getActionName());
		$condition = "id ={$date['id']}";
		if($model->forbid($condition)){
			$this->assign("message",'状态禁用成功！');
			$this->assign("jumpUrl",$this->getReturnUrl());
		}else {
			$this->assign('error','状态禁用失败！');
		}
		$this->forward();
	}
	
	
	/**
	 * 
	 * Enter description here ...
	 */
	function getReturnUrl(){
		return __URL__.'?'.C('VAR_MODULE').'='.MODULE_NAME.'&'.C('VAR_ACTION').'='.C('DEFAULT_ACTION');
	}

	
	
	
	
	
	
	
	
	
	/**
	 * 分页处理
	 * bug：检索条件分页带不过来
	 * Enter description here ...
	 * @param unknown_type $model
	 * @param unknown_type $data
	 * @param unknown_type $page
	 * @param unknown_type $num
	 * @param unknown_type $where
	 */
	function page($model,&$data,&$page,$num = 10,$where){
		if(empty($num))
			$num = 10;  //分页数
			
		import("@.ORG.Page");  //引入分页类

		$count = $model->where($where)->count(); //总数
		
		$p = new Page ($count,$num);
		$data=$model->where($where)->limit($p->firstRow.','.$p->listRows)->order($model->getPk().' desc')->findAll();
		
		$page = $p->show ();
	}
	
	
	/**
	 * 新建
	 * Enter description here ...
	 */
	public function add() {
		$this->display();
	}
	
	
	/**
	 * 新建保存
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function insert($data){
			
		//Log::write('...新建保存start....：' , Log::DEBUG);
		
		$model = D($this->getActionName());
		$data = $model->create($data,1);
		if(empty($data) ||$data === false) {
			$this->error($model->getError());
		}
		$id = $model->add($data);
		if($id) {
			$this->assign('jumpUrl','__URL__/index');
			$this->success("添加成功");
		}else {
			$this->error($model->getError());
		}
		
		//Log::write('...新建保存end....：' , Log::DEBUG);
	}
	
	
	
	/**
	 * 修改
	 * Enter description here ...
	 */
	public function edit() {
		$model	= D($this->getActionName());
		$id     = GetNum($_GET[$model->getPk()]);
		
		//dump($model);
		
		//Log::write('...修改获取信息start....：'.$this->getActionName().'  主键: '.$id , Log::DEBUG);
		
		$vo	=	$model->find($id);
		
		$this->assign('data',$vo);
		
		$this->display();
		
		
		//Log::write('...修改获取信息end....：', Log::DEBUG);
		
	}
	
	
	/**
	 * 
	 * 修改保存
	 * @param unknown_type $data
	 * @param unknown_type $where
	 */
	public function update($data,$where){
		//Log::write('...修改保存start....：' , Log::DEBUG);
			
		$model = D($this->getActionName());
		$data = $model->create($data,2);
		if(empty($data) ||$data === false) {
			$this->error($model->getError());
		}
		$id = $model->where($where)->save($data);
		if($id) {
			$this->assign('jumpUrl','__URL__/index');
			$this->success("编辑成功");
		}else {
			$error = $model->getError();
			if (empty($error)) {
				$this->assign('jumpUrl','__URL__/index');
				$this->success("编辑成功");
			}else {
				$this->error($error);
			}
		}
		//	Log::write('...修改保存end....：' , Log::DEBUG);
	}
	
	/**
	 * 删除
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function del($data){
		$model        = D($this->getActionName());
		if(!empty($model)) {
			if (empty($data)) {
				$id       = GetNum($_GET[$model->getPk()]);  //主键
			}else {
				$id       = $data;
			}
			
			//Log::write('...删除start....主键：' .$id , Log::DEBUG);
			
			
			if(isset($id)) {
				if($model->delete($id)){
					$this->success("删除成功");
				}else {
					$this->error("删除失败");
				}
			}else {
				$this->error('非法操作');
			}
			
			//Log::write('...删除end....：' , Log::DEBUG);
		}
		
		
	}
	
	/**
	 * 删除多个
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function dels($data){
		$model        = D($this->getActionName());
		if(!empty($model)) {
			if (empty($data)) {
				$ids       = getdotstring($_POST[$model->getPk().'s'],'int');
			}else {
				$ids       = getdotstring($data,'int');
			}
			
			//	Log::write('...删除多个start....：' .$ids , Log::DEBUG);
			
			
			if(empty($ids))$this->error('未选定任何项');
			$where[$model->getPk()] = array('in',"{$ids}");
			if ($model->where($where)->delete()){
				$this->success('已删除选定项');
			}else{
				$this->error($model->getError());
			}
			
			//	Log::write('...删除多个end....：' , Log::DEBUG);
		}

	}
	
	
	
	
	
	
}

?>